package unimelb.filesync.common;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import unimelb.filesync.common.misc.ProtocolContext;
import unimelb.filesync.common.protocol.CompositePhase;
import unimelb.filesync.common.protocol.IFileSyncPhase;

public abstract class FileSyncApp {

    private IFileSyncPhase phaseChain;

    public void run(String[] args) {
        setUp(args);
        phaseChain.execute();
    }

    private void setUp(String[] args) {
        final Map<String, Object> params = toParamMap(args);

        configProtocolContext(params);

        initProtocolPhaseChain();
    }

    private void configProtocolContext(Map<String, Object> params) {
        for (Entry<String, Object> param : params.entrySet()) {
            getProtocolContext().addObject(param.getKey(), param.getValue());
        }
    }

    private void initProtocolPhaseChain() {
        phaseChain = new CompositePhase(createPhaseList());
    }

    protected abstract Map<String, Object> toParamMap(String[] args);

    protected abstract ProtocolContext getProtocolContext();

    protected abstract List<IFileSyncPhase> createPhaseList();

}
